package com.joshondesign.sdljava.test;

import com.joshondesign.sdljava.*;
import java.util.Random;

public class main {
        
    private static void setupSDL() {
       p("before");
        SDL_version version = SDL.SDL_Linked_Version();
        p("major = " + version.getMajor() 
            + "." + version.getMinor()
            + "." + version.getPatch());
        
        p("trying to init sdl");
        if(SDL.SDL_Init(SDL.SDL_INIT_VIDEO) < 0) {
            p("init failed!");
            System.exit(1);
        } else {
            p("init succeeded");
        }
        
        SDL_VideoInfo info = SDL.SDL_GetVideoInfo();
        p("video info = " + info);
        p("hw avail = " + info.getHw_available());
        p("wm avail = " + info.getWm_available());
        p("hw blit fast = " + info.getBlit_hw());
        p("currentColor fill fast = " + info.getBlit_fill());
        p("total video mem = " + info.getVideo_mem());
        p("width = " + info.getCurrent_w());
        p("height = " + info.getCurrent_h());
        
        SDL_PixelFormat format = info.getVfmt();
        p("bits per pixel = " + format.getBitsPerPixel());
        p("red mask = " + Long.toHexString(format.getRmask()));
        p("red shift = " + format.getRshift());
        p("green mask = " + Long.toHexString(format.getGmask()));
        p("green shift = " + format.getGshift());
        p("blue mask = " + Long.toHexString(format.getBmask()));
        p("blue shift = " + format.getBshift());
        p("alpha mask = " + Long.toHexString(format.getAmask()));
        p("alpha shift = " + format.getAshift());
        
        p("creating surface");
        SDL_Surface surface = SDL.SDL_SetVideoMode(
            info.getCurrent_w()
            ,info.getCurrent_h()
            ,0
            ,SDL.SDL_SWSURFACE);
        if(surface == null) {
            p("couldn't create a surface");
            SDL.SDL_Quit();
            System.exit(1);
        }
        
        p("w & h = " + surface.getW() + " " + surface.getH());
        p("pitch = " + surface.getPitch());
        p("offset = " + surface.getOffset());
        p("flags = " + Long.toHexString(surface.getFlags()));
        SDL_Rect clip = surface.getClip_rect();
        p("cliprect = " + clip.getX() + " " + clip.getY() + " " + clip.getW() + " " + clip.getH());
        p("bits per pixel =  " + surface.getFormat().getBitsPerPixel());
        p("bytes per pixel =  " + surface.getFormat().getBytesPerPixel());
        p("pixels = " + surface.getPixels());
        
        SDL.SDL_LockSurface(surface);
        try {
            
            SDL.setPixel(surface,10,10,0xFFFFFFFF);
            
            //this is the little endian version
            //blue
            for(int i=0; i<256; i++) {
                for(int j=0; j<100; j++) {
                    SDL.setPixel(surface, i,j,     0x000000FF | (i<<24));
                }
            }
            //green
            for(int i=0; i<256; i++) {
                for(int j=0; j<100; j++) {
                    SDL.setPixel(surface, i,j+100, 0x000000FF | (i<<16));
                }
            }
            //red
            for(int i=0; i<256; i++) {
                for(int j=0; j<100; j++) {
                    SDL.setPixel(surface, i,j+200, 0x000000FF | (i<<8));
                }
            }
            //        p("buffer = " + SDL.SWIG_getPixelDirectByteBuffer(surface));
            //        java.nio.ByteBuffer pixels = (java.nio.ByteBuffer)
            //            SDL.SWIG_getPixelDirectByteBuffer(surface);
            SDL.SDL_UpdateRect(surface,0,0,256,300);
            
            //p("direct = " + pixels.isDirect());
            SDL.SDL_UnlockSurface(surface);
        
            try {
            Thread.currentThread().sleep(10*1000);
            } catch(Exception ex) {
                ex.printStackTrace();
            }
        
        } catch(Exception ex) {
            ex.printStackTrace();
        }
        SDL.SDL_Quit();
        p("quit SDL");
    }
    
    
    public static void main(String ... args) {
        p("really before");
        SDLUtil.InitCallback callback = new SDLUtil.InitCallback() {
                public void callback() {
                    System.out.println("in the java callback");
                    setupSDL();
                }
        };
        
        SDLUtil.init(callback);        
     }
    
    public static void p(String s) {
        //System.out.println(s);
        System.err.println(s);
    }
}
